package com.jxsd.edu.cn.tools;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



public class DbSource 
{
	private  static  Log  logger=LogFactory.getLog("DAO");
	
	private  static  String   uri="java:/comp/env/jdbc/jxsdmarket";
	
	//获取一个数据库连接
	  public  static Connection  getConnection() 
	  {  
		  Connection conn=null;
		  
		  Context ctx;
		   try 
		   {
			 ctx = new InitialContext();
			 DataSource ds = (DataSource)ctx.lookup(uri);
		     try {
				conn=ds.getConnection();
			} catch (SQLException e) {
				logger.debug("DbSource.getConnection().SQLException");
			}
		   } catch (NamingException e) {
			   logger.debug("DbSource.getConnection().NamingException");
		}
		  return  conn;
	  }
	
	
	//关闭数据库连接
	  public static void  close(Statement stmt,Connection conn,ResultSet result)
	  {
		  try {
		  if(result!=null)
			  result.close();
		  if(stmt!=null)
			  stmt.close();
		  if(conn!=null)
			 conn.close();
			} catch (SQLException e) {
				logger.debug("DbSource.close(Statement stmt,Connection conn,ResultSet result).SQLException");
			}
	   }
}
